import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
			total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
      search: '',
      reviewValidate: {}, //项目评审校验对象
      deadlineRectificationValidate: {}, //项目限期整改校验对象
      expertSearchType: 'expertName',//专家搜索类型
      expertSearch: '',//专家搜索信息
      notApplyExpertTotal: 0, //非申请专家总条数
      notApplyExpertPage: 1, //非申请专家当前页
      notApplyExpertSize: 5, //非申请专家每页显示条数
      applyExpertTotal: 0, //申请专家总条数
      applyExpertPage: 1, //申请专家当前页
      applyExpertSize: 10, //申请专家每页显示条数
      projectInfo: {}, //项目
      deadlineRectification: {}, //项目限期整改
      reviewExpertList: [], //评审专家
      notApplyExpertList: [], //非申请专家列表
      applyExpertList: [], //申请专家列表
      //projectApplicationList: [], //申请项目分页数据
      projectList: [], //项目分页数据
      projectReviewResultList: [], //项目评审结果信息
      projectReviewResultList2: [], //第二次项目评审结果信息
      reviewerExpertTemp: {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      }, //临时评审专家
			demandModelShow: false,
			reviewTable: true,
			score: false,
			paperWorkBox: false,
			pingJiaZhiBox: false,
			Specialist: false,
			Tickets: false,
      auditModel: false,
			uploadNum: 0,
			FileUpload: [],
      //新增评审相关对话框状态
      organizationReviewDialogFlag: false,
      organizationReviewNextDialogFlag: false,
      closureAuditMsg:false,//判断不通过内容是否输入
      curIndex:-1,//选中下标
      reSubmit:true,//是否能请求通知评审专家的接口
      auditObj: {
      	closureAuditMsg:""
      }, //审核对象
      pickerOptions1: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      pickerOptions2: {
        disabledDate: (time) => {
          if (this.deadlineRectification.deadlineBeginTime) {
            return time.getTime() < this.deadlineRectification.deadlineBeginTime || time.getTime() > this.deadlineRectification.deadlineBeginTime.getTime() + 180 * 24 * 3600 * 1000
          } else {
            return time.getTime() < Date.now() - 8.64e7;
          }
        }
      },
    }
	},
	components: {
		pageination
	},
	created() { //调用方法
    //初始化分页信息
    //this.searchTodo();
	},
	mounted() {
    let that = this;

    //加载评审表单验证
    that.reviewValidate = $("#reviewForm").validate({
      ignore: [],
      rules: {
        standardUrl: { // 评审标准地址
          required: true
        },
        endTime: { // 评审截止时间
          required: true
        },
        type: { // 评审类型
          required: true
        }
      },
      messages: {
        standardUrl: { // 评审标准地址
          required: "请上传评审标准"
        },
        endTime: { // 评审截止时间
          required: "请选择评审截止时间"
        },
        type: { // 评审类型
          required: "请选择评审类型"
        }
      },
      /*errorElement: "em",
      errorPlacement: function (error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

    //加载限期整改表单验证
    that.deadlineRectificationValidate = $("#deadlineRectificationForm").validate({
      ignore: [],
      rules: {
        deadlineBeginTime: { // 限期整改开始时间
          required: true
        },
        deadlineEndTime: { // 限期整改结束时间
          required: true
        },
        closureAuditMsg:{//
        	required: true,
        	maxlength:100
        },
        closureAuditEnclosureUrl:{//
          maxlength:500
        }
      },
      messages: {
        deadlineBeginTime: { // 限期整改开始时间
          required: "请选择开始时间"
        },
        deadlineEndTime: { // 限期整改结束时间
          required: "请选择结束时间"
        },
        closureAuditMsg:{//
        	required: "请输入整改内容",
        	maxlength: "整改内容最多只能输入100个字"
        },
        closureAuditEnclosureUrl:{//
          maxlength: "附件地址最多只能有500个字"
        }
      },
      /*errorElement: "em",
      errorPlacement: function (error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
      //初始化分页信息
      this.searchTodo();
		},
    //页码切换执行方法
    applyExpertPageFn: function(val, pid) {
      this.applyExpertPage = parseInt(val);
      //此处调列表数据接口
      this.searchAllApplyExpert(this.projectInfo.id);
    },
    //页码切换执行方法
    notApplyExpertPageFn: function(val, pid) {
      this.notApplyExpertPage = parseInt(val);
      //此处调列表数据接口
      this.searchAllNotApplicationExpert(this.projectInfo.id);
    },
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
    //显示组织评审的下一步对话框
    showOrganizationReviewNextDialog: function() {
      if (this.reviewExpertList == null || this.reviewExpertList.length == 0) {
        layer.msg("请选择评审专家！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      this.organizationReviewDialogFlag = false;
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
      this.FileUpload = [];
      this.applyExpertList = [];
      this.applyExpertTotal =  0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.searchAllApplyExpert(this.projectInfo.id);
      this.organizationReviewNextDialogFlag = true;
    },
    //删除附件
    deleteEnclosure: function() {
      this.FileUpload = [];
      this.auditObj.screenAuditEnclosureUrl = "";
      this.reviewerExpertTemp.standardUrl = "";
      this.auditObj.closureAuditEnclosureUrl = "";
      this.deadlineRectification.closureAuditEnclosureUrl = '';
      $("#standardUrl-error").css("display","block");
    },
    /**文件上传js**/
    idFrontImg2: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if (res.success) {
            let fileInfo = res.data[0];
            that.auditObj.closureAuditEnclosureUrl = fileInfo.url;
            that.deadlineRectification.closureAuditEnclosureUrl = fileInfo.url;
            that.FileUpload = [that.getFileName(fileInfo.url)];
          }
        })
    },
		/**文件上传js**/
		idFrontImg: function() {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfo = res.data[0];
          that.FileUpload = [that.getFileName(fileInfo.url)];
          that.reviewerExpertTemp.standardUrl = fileInfo.url;
          $("#standardUrl-error").css("display","none");
        }
      })
		},
		//评审组织模态框
		demand: function(projectId) {
      /*this.demandModelShow = true;
      this.reviewTable = true;
      this.FileUpload = [];
      this.reviewExpertList = [];
      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.score = false;
      this.searchProject(projectId);
      this.searchAllApplyExpert(projectId);
      this.searchAllNotApplicationExpert(projectId);*/

      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.searchProject(projectId);
      //清空评审专家筛选
      this.notApplyExpertList = [];
      this.reviewExpertList = [];
      this.notApplyExpertTotal = 0; //非申请专家总条数
      this.notApplyExpertPage = 1; //非申请专家当前页
      this.notApplyExpertSize = 5; //非申请专家每页显示条数
      this.expertSearchType = 'expertName';
      this.expertSearch = '';
      this.searchAllNotApplicationExpert(projectId);
      this.organizationReviewDialogFlag = true;
		},
    auditModelShow: function(projectId) {
      this.auditObj = {};
      this.FileUpload = [];
      this.auditObj.id = projectId;
      this.auditModel = true;
    },
    auditData: function() {
      let auditObj = this.auditObj;
      this.closureAuditMsg=true;
      if (auditObj.closureAuditMsg == null || auditObj.closureAuditMsg.length == 0 || auditObj.closureAuditMsg.length > 100) {
        return;
      }
      this.auditProject(auditObj.id, 3, auditObj.closureAuditMsg, auditObj.closureAuditEnclosureUrl);
      this.auditModel = false;
      this.closureAuditMsg=false;
    },
		//关闭模态框
		closeModel: function(modelName) {
      $(".secondLevelPageination .jump_page>input").val("");
      if (modelName == null || modelName == "") {
        this.Specialist = false;
        this.demandModelShow = false;
        this.Tickets = false;
        this.closureAuditMsg=false;

        this.auditModel = false;
        this.organizationReviewDialogFlag = false;
        this.organizationReviewNextDialogFlag = false;
      } else {
        switch (modelName) {
          case 'Specialist':
            this.Specialist = false;
            break;
          case 'demandModelShow':
            this.demandModelShow = false;
            break;
          case 'Tickets':
            this.Tickets = false;
            break;
          case 'closureAuditMsg':
            this.closureAuditMsg = false;
            break;
          case 'auditModel':
            this.auditModel = false;
            break;
          case 'organizationReviewDialogFlag':
            this.organizationReviewDialogFlag = false;
            break;
          case 'organizationReviewNextDialogFlag':
            this.organizationReviewNextDialogFlag = false;
            break;
        }
      }

//
//			this.WorkModelShow = false;
		},
		//组织评审下一步
		nextStep: function() {
      //校验是否选择评审专家
      if (this.reviewExpertList == null || this.reviewExpertList.length == 0) {
        layer.msg("请选择评审专家！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
		  //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
		  this.reviewTable = false;
			this.score = true;
		},
		//限期整改模态框
		SpecialistBtn: function(id) {
      this.deadlineRectification = {};
      this.FileUpload = [];
		  //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#deadlineRectificationForm").validate().resetForm();
		  if (id != null && id != "") {
			  this.Specialist = true;
        this.deadlineRectification.id = id;
      }
		},
		//评审结果模态框
		AllTickets: function(projectId) {
			this.Tickets = true;
      this.searchProjectReviewResult(projectId);
		},
		//打分制
		paperWork: function() {
			this.paperWorkBox = true;
			this.pingJiaZhiBox = false;
		},
		//评价制
		pingJiaZhi: function() {
			this.pingJiaZhiBox = true;
			this.paperWorkBox = false;
		},
    //重新评价
    reappraisal: function(projectId) {
      this.Tickets = false;
      this.demand(projectId);
      this.searchAllReviewExpert(projectId,0,1);

      this.organizationReviewDialogFlag = false;
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
      this.FileUpload = [];
      this.applyExpertList = [];
      this.applyExpertTotal =  0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.searchAllApplyExpert(projectId);
      this.organizationReviewNextDialogFlag = true;
    },
    deadlineBeginTimeChange: function(event) {
		  $("input[name='deadlineBeginTime']").val(event);
      $("input[name='deadlineBeginTime']").valid();
    },
    deadlineEndTime: function(event){
      $("input[name='deadlineEndTime']").val(event);
      $("input[name='deadlineEndTime']").valid();
    },
    //更换专家
    replaceExpert: function(projectId) {
      //this.Tickets = false;
      this.demand(projectId);

      //清空评审专家筛选
      this.notApplyExpertList = [];
      this.reviewExpertList = [];
      this.notApplyExpertTotal = 0; //非申请专家总条数
      this.notApplyExpertPage = 1; //非申请专家当前页
      this.notApplyExpertSize = 5; //非申请专家每页显示条数
      this.expertSearchType = 'expertName';
      this.expertSearch = '';
      this.searchAllNotApplicationExpert(projectId);
      this.organizationReviewDialogFlag = true;
    },
    //新增评审相关对话框显示状态
    //显示组织评审的下一步对话框
    showOrganizationReviewNextDialog: function() {
      if (this.reviewExpertList == null || this.reviewExpertList.length == 0) {
        layer.msg("请选择评审专家！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      //this.organizationReviewDialogFlag = false;
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
      this.FileUpload = [];
      this.applyExpertList = [];
      this.applyExpertTotal =  0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.searchAllApplyExpert(this.projectInfo.id);
      this.organizationReviewNextDialogFlag = true;
    },
    //业务操作
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    //审核状态
    //结题审核状态（1：待审核；2：审核通过；3：审核不通过，4：限期整改）
    auditStatus: function(status) {
      if (status != null) {
        switch (status) {
          case 0:
            return "暂存";
          case 1:
            return "待审核";
          case 2:
            return "通过";
          case 3:
            return "不通过";
          case 4:
            return "限期整改";
        }
      }
    },
    //查询所有评审专家
    searchAllReviewExpert: function(projectId,matters,reviewNum) {
      let that = this;
      if (projectId != null && matters != null && reviewNum != null) {
        that.reviewExpertList = [];
        that.$post(
          that.urls.getAllReviewExpert,
          {"projectId": projectId, "reviewNum": reviewNum, "matters": matters}
        )
          .then(function(res) {
            if(res.success) {
              let reviewExpertList = [];
              let notApplyExpertList = that.notApplyExpertList;
              let expertList = res.data;
              for (var i = 0,len = expertList.length;i < len;i++) {
                for (var j = 0,len2 = notApplyExpertList.length;j < len2;j++) {
                  if (expertList[i].id == notApplyExpertList[j].expertId) {
                    reviewExpertList.push(notApplyExpertList[j]);
                  }
                }
              }
              that.reviewExpertList = reviewExpertList;
            }
          })
      }
    },
    //查询评审信息
    searchProjectReviewResult: function(projectId) {
      let that = this;
      if (projectId != null) {
        that.projectReviewResultList = [];
        that.projectReviewResultList2 = [];
        //查询第一次评审
        that.$post(
          that.urls.getAllProjectReviewResult,
          {"projectId": projectId, "reviewNum": 1, "matters": 1}
        )
          .then(function(res) {
            if(res.success) {
              that.projectReviewResultList = res.data;
            }
          })
        //查询第二次评审
        that.$post(
          that.urls.getAllProjectReviewResult,
          {"projectId": projectId, "reviewNum": 2, "matters": 1}
        )
          .then(function(res) {
            if(res.success) {
              that.projectReviewResultList2 = res.data;
            }
          })
      }
    },
    //查询项目
    searchProject: function(id) {
      let that = this;
      that.projectInfo = {};
      if (id != null) {
        that.$post(
          that.urls.getProjectVOReview,
          {"id": id, "reviewMatters": 1}
        )
        .then(function(res) {
          if(res.success) {
            that.projectInfo = res.data;
          }
        })
      }
    },
    //搜索数据列表
    searchTodo: function() {
      let that = this;
      that.$post(
        that.urls.getProjectClosurePageInfo,
        {"pageNum": that.page,"pageSize": that.size,"search": that.search}
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //审核
    auditProject: function(id, closureAuditStatus, closureAuditMsg, closureAuditEnclosureUrl) {
      if (id == null || closureAuditStatus == null) {
        return;
      }
      let that = this;
      that.$post(
        that.urls.auditClosureProject,
        {"id": id, "closureAuditStatus": closureAuditStatus,"closureAuditMsg": closureAuditMsg,"closureAuditEnclosureUrl": closureAuditEnclosureUrl}
      )
      .then(function(res) {
        let msg = "";
        if(res.success) {
          msg = '审核成功！';
          //重新渲染数据
          that.searchTodo();
        } else {
          msg = '审核失败！';
        }
        layer.msg(msg, {
          time: 3000, //3s后自动关闭
        });
      })
    },
    //搜索所有非申请专家信息列表
    searchAllNotApplicationExpertTodo: function() {
      this.searchAllNotApplicationExpert(this.projectInfo.id);
    },
    //搜索所有申请专家未审核信息列表
    searchAllApplyExpert: function(projectId) {
      let that = this;
      that.applyExpertList = [];
      /*that.$post(
        that.urls.getAllApplicationExpert,
        {"matters": 1,"projectId": projectId}
      )
      .then(function(res) {
        if(res.success) {
          that.applyExpertList = res.data;
        }
      })*/
      that.$post(
        //that.urls.getALLApplicationExpert,
        that.urls.getApplicationExpertPageInfo,
        {"matters": 1,"projectId": projectId,"pageNum": that.applyExpertPage,"pageSize": that.applyExpertSize}
      )
      .then(function(res) {
        if(res.success) {
          //that.applyExpertList = res.data;
          that.applyExpertList = res.pageInfo.list;
          that.applyExpertTotal = res.pageInfo.total;
          that.applyExpertPage = res.pageInfo.pageNum;
        }
      })
    },
    //搜索所有非对接专家信息列表
    searchAllNotApplicationExpert: function(projectId) {
      let that = this;
      that.notApplyExpertLis = [];
      let obj = {};
      obj["projectId"] = projectId;
      obj["pageNum"] = that.notApplyExpertPage;
      obj["pageSize"] = that.notApplyExpertSize;
      obj[that.expertSearchType] = that.expertSearch;
      that.$post(
        that.urls.getNotDockingExpertPageInfo,
        obj
      )
      .then(function(res) {
        if(res.success) {
          //that.notApplyExpertList = res.data;
          that.notApplyExpertList = res.pageInfo.list;
          that.notApplyExpertTotal = res.pageInfo.total;
          that.notApplyExpertPage = res.pageInfo.pageNum;
        }
      })
    },
    //添加审核专家
    addReviewExpert: function(expert) {
      let flg = true; //是否是新数据
      let reviewExpertList = this.reviewExpertList;
      for (let i = 0,len = reviewExpertList.length;i < len;i++) {
        if (reviewExpertList[i].expertId == expert.expertId) {
          flg = false;
          break;
        }
      }
      if (expert != null && flg) {
        this.reviewExpertList.push(expert);
      }
    },
    //删除审核专家
    removeReviewExpert: function(obj) {
      if (obj != null) {
        let reviewExpertList = this.reviewExpertList;
        for (let i = 0,len = reviewExpertList.length;i < len;i++) {
          if (reviewExpertList[i].expertId == obj.expertId) {
            this.reviewExpertList.splice(i,1);
            break;
          }
        }
      }
    },
    //保存项目限期整改
    saveProjectDeadlineRectification: function() {
      let that = this;
      if (that.deadlineRectification == null || that.deadlineRectification.deadlineEndTime < that.deadlineRectification.deadlineBeginTime) {
        layer.msg("结束时间不能早于开始时间！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      //校验限期整改验证表单
      let flg = that.deadlineRectificationValidate.form();
      console.log(flg)

      if (!flg) {
        return;
      }
       console.log(flg+"sss")
      that.$post(
        that.urls.addProjectDeadlineRectification,
        that.deadlineRectification
      )
      .then(function(res) {
        let msg = "";
        if(res.success) {
          msg = '添加限期整改成功！';
        } else {
          msg = '添加限期整改失败！';
        }
        layer.msg(msg, {
          time: 3000, //3s后自动关闭
        });
        that.searchTodo();
        that.closeModel();
      })
    },
    //保存评审信息
    saveReviewerExpert: function() {
      let that = this;
      //校验评审表单数据
      let flg = that.reviewValidate.form();
      if (!flg) {
        return;
      }
      let reviewExpertIds = []; //评审专家id集合
      let projectApplicationIds = []; //申请专家id集合
      let reviewExpertList = that.reviewExpertList; //评审专家信息集合
      let applyExpertList = that.applyExpertList; //评审专家信息集合
      //校验评审专家和申请专家不能为空
      if (reviewExpertList == null || applyExpertList == null || reviewExpertList.length == 0 || applyExpertList.length == 0) {
        layer.msg("请选择评审专家和者申请专家", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      for (let i = 0,len = reviewExpertList.length;i < len;i++) {
        reviewExpertIds.push(reviewExpertList[i].expertId)
      }
      for (let i = 0,len = applyExpertList.length;i < len;i++) {
        projectApplicationIds.push(applyExpertList[i].id)
      }
      let obj = that.reviewerExpertTemp;
      //评审专家id
      obj["reviewExpertIds"] = reviewExpertIds;
      //申请专家id
      obj["projectApplicationIds"] = projectApplicationIds;
      //项目id
      obj["projectId"] = this.projectInfo.id;
      //评审事宜（0：项目申请；1：项目结题）
      obj["matters"] = 1;
      //评审次数
      obj["reviewNum"] = this.projectInfo.reviewNum == null ? 1 : this.projectInfo.reviewNum + 1;
      if(!that.reSubmit){
      	return;
      }
      that.reSubmit=!that.reSubmit;
      that.$post(
        that.urls.batchAddProjectReview,
        obj
      )
      .then(function(res) {
      	that.reSubmit=!that.reSubmit;
        let msg = "";
        if(res.success) {
          msg = '通知审核成功！';
          that.searchTodo();
          that.closeModel();
        } else {
          msg = '通知审核失败！';
        }
        layer.msg(msg, {
          time: 3000, //3s后自动关闭
        });
      })
    },
	}
}
